package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qugr
 * @create 2020-07-10 18:19
 */
@RestController
@RequestMapping("/user")
public class RegisterController {

    @Reference(timeout = 6000,check = false,retries = 0)
    IRegisterService registerService;

    @Anoymous
    @PostMapping("/register")
    public ResponseData userRegister(@RequestBody UserRegisterRequest request, HttpServletRequest httpServletRequest) {
        String uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(request.getCaptcha());
        //用于发送邮件时的内容
        Map<String,String> requestMap = new HashMap<>();
        requestMap.put("localAddr",httpServletRequest.getLocalAddr());
        requestMap.put("localPort",httpServletRequest.getLocalPort() + "");
        UserRegisterResponse response = registerService.userRegister(request,kaptchaCodeRequest,requestMap);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @Anoymous
    @GetMapping("/verify")
    public ResponseData userVerify(String uuid, String username) {
        UserVerifyRequest request = new UserVerifyRequest();
        request.setUuid(uuid);
        request.setUserName(username);
        UserVerifyResponse response = registerService.userVerify(request);

        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
